package com.yc.services;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.bean.Banner;
import com.yc.mapper.BannerMapper;
import com.yc.web.DTO.BannerDTO;
import com.yc.web.clients.IdGeneratorClient;
import com.yc.web.clients.OSSClient;
import com.yc.web.controller.model.ResponseResult;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Log
public class BannerServiceImpl implements BannerService{

    private final BannerMapper bannerMapper;
    private final OSSClient ossClient;
    private final IdGeneratorClient idGeneratorClient;

    public final static Integer BANNER_STATUS_ENABLE = 1;
    public final static Integer BANNER_STATUS_DISABLE = 0;

    @Override
    @Transactional
    public BannerDTO addBanner(BannerDTO bannerDTO) {
        // 1. 生成类别ID
        ResponseResult rr= this.idGeneratorClient.getNextId();
        if(  rr.getCode()!=1){
            throw new RuntimeException("轮播图ID生成失败");
        }
        Long bannerId =Long.parseLong(rr.getData().toString());
        bannerDTO.setBannerId(bannerId);

        // 2. 模拟图标上传并设置URL
        ResponseResult rr2= this.ossClient.uploadFile(   new MultipartFile[]{ bannerDTO.getImageUrlFile() });
        if(  rr2.getCode()!=1){
            throw new RuntimeException("轮播图图片上传失败");
        }
        List<String> mainImages= (List<String>) rr2.getData();
        bannerDTO.setImageUrl(   mainImages.get(0)  );

        if (bannerDTO.getBannerStatus() == null) {
            bannerDTO.setBannerStatus(  BANNER_STATUS_ENABLE  ); // 默认正常
        }
        // 4. 将DTO转换为实体
        Banner banner = new Banner();
        // spring 提供一个工具类，用于完成 对象的属性拷贝
        BeanUtils.copyProperties(bannerDTO, banner);
        // 5. 插入数据库
        int result = this.bannerMapper.insert(banner);
        if (result > 0) {
            // 插入成功后，返回包含完整信息的DTO
            BeanUtils.copyProperties(banner, bannerDTO);

            return bannerDTO;
        } else {
            throw new RuntimeException("新增类别失败");
        }
    }

    @Override
    public List<BannerDTO> getBannersByStatus(Integer bannerStatus) {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        if( bannerStatus!=null&& bannerStatus!=-1 ){
            queryWrapper.eq(Banner::getBannerStatus,bannerStatus);  // where bannerstatus=#bannerStatus
        }
        // 将实体列表转换为DTO列表
        List<Banner> banners = this.bannerMapper.selectList(queryWrapper);
        return banners.stream()
                .map(banner -> {
                    BannerDTO dto = new BannerDTO();
                    BeanUtils.copyProperties(banner, dto);
                    return dto;
                })
                .collect(Collectors.toList());
    }
}
